package cn.itsource.ymcc.web.controller;

import cn.itsource.result.JSONResult;
import cn.itsource.ymcc.dto.MessageDto;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/messageProducer")
public class MessageProducerController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 公共服务公共接口-消息生产者-主要用来推送消息 - 被其它微服务使用openfeign调用
     *  支持邮件、短信、站内信
     * @param messageDto
     * @return
     */
    @PostMapping("/sendMsg")
    public JSONResult sendMsg(@RequestBody MessageDto messageDto){
        SendResult sendResult = rocketMQTemplate.syncSend(messageDto.getTopic() + ":" + messageDto.getTags(),
                MessageBuilder.withPayload(JSON.toJSONString(messageDto)).build());
        return sendResult.getSendStatus() == SendStatus.SEND_OK ? JSONResult.success() : JSONResult.error();
    }

}
